package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.LabelNewDao;
import cc.linker.steplink.crm.dao.LabelRelationDao;
import cc.linker.steplink.crm.domain.LabelPo;
import cc.linker.steplink.crm.domain.TacticsPo;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseException;
import cc.linker.steplink.crm.service.ILabelNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LabelNewServiceImpl implements ILabelNewService {

    @Autowired
    private LabelNewDao labelNewDao;
    @Autowired
    private LabelRelationDao labelRelationDao;


    @Override
    public List<LabelPo> listLabel(Map<String, Object> labelMap) {
        return  labelNewDao.listLabel(labelMap);
    }

    @Override
    public LabelPo getLabel(String labelId) {
        if (StringUtils.isEmpty(labelId)) {
            throw new GlobalException(new ResponseException(400, "标签ID不能为空"));
        }
        return  labelNewDao.getLabel(labelId);
    }

    @Override
    public Boolean addLabel(LabelPo record) {
        if(StringUtils.isEmpty(record.getLabelName())){
            throw new GlobalException(new ResponseException(400, "标签名不能为空"));
        }
        if(labelNewDao.ifLabelExist(record)>0){
            throw new GlobalException(new ResponseException(400, "标签重复，请修改"));
        }
        int id = labelNewDao.insert(record);
        if(id>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "添加标签失败"));
    }

    @Override
    public Boolean updateLabel(LabelPo record) {
        if(StringUtils.isEmpty(record.getLabelName())){
            throw new GlobalException(new ResponseException(400, "标签名不能为空"));
        }
        if(StringUtils.isEmpty(record.getLabelId())){
            throw new GlobalException(new ResponseException(400, "标签id不能为空"));
        }
        if(labelNewDao.ifLabelExist(record)>0){
            throw new GlobalException(new ResponseException(400, "标签重复，请修改"));
        }
        int count = labelNewDao.updateByPrimaryKeySelective(record);
        if(count>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "更新标签失败"));
    }

    @Override
    public Boolean delLabel(String labelId) {
        //查询是否关联策略
        int tacticsRelCount = labelNewDao.tacticsRelCount(labelId);
        if(tacticsRelCount>0){
            throw new GlobalException(new ResponseException(400, "此标签正在被自动标签策略引用，请删除策略后再进行删除"));
        }
        int delCount = labelNewDao.deleteByPrimaryKey(labelId);
        if(delCount>0){
            //删除关联记录
            labelRelationDao.deleteByLabelId(labelId);
            labelNewDao.deleteGoodsRel(labelId);
            labelNewDao.deleteTacticsRel(labelId);
            return true;
        }
        throw new GlobalException(new ResponseException(400, "删除标签失败"));
    }

    @Override
    public List<TacticsPo> tacticsList(Map<String, Object> tacticsMap) {
        return  labelNewDao.tacticsList(tacticsMap);
    }

    @Override
    public Boolean addTactics(Map<String, Object> tactics) {
        TacticsPo tacticsPo = new TacticsPo();
        String tacticsName = (String) tactics.get("tacticsName");
        tacticsPo.setTacticsName(tacticsName);
        String orgId = (String) tactics.get("orgId");
        tacticsPo.setOrgId(orgId);
        String autoSetup = (String) tactics.get("autoSetup");
        tacticsPo.setAutoSetup(autoSetup);
        Integer autoNum = (Integer) tactics.get("autoNum");
        tacticsPo.setAutoNum(autoNum);
        String labelIds = "";
        if(!StringUtils.isEmpty(tactics.get("labelIds"))){
            labelIds = tactics.get("labelIds").toString();
        }else {
            throw new GlobalException(new ResponseException(400, "请选择标签"));
        }
        tacticsPo.setStatus(0);
        int count = labelNewDao.insertTactics(tacticsPo);
        if(count>0){
            String[] labelIdList = labelIds.split(",");
            Map map =new HashMap();
            map.put("tacticsId",tacticsPo.getTacticsId());
            map.put("labelIdList",labelIdList);
            labelNewDao.insertTacticsRel(map);
            return true;
        }
        throw new GlobalException(new ResponseException(400, "添加策略失败"));
    }

    @Override
    public Boolean updateTactics(Map<String, Object> tactics) {
        TacticsPo tacticsPo = new TacticsPo();
        Long tacticsId = Long.valueOf(tactics.get("tacticsId").toString());
        tacticsPo.setTacticsId(tacticsId);
        String tacticsName = (String) tactics.get("tacticsName");
        tacticsPo.setTacticsName(tacticsName);
        String autoSetup = (String) tactics.get("autoSetup");
        tacticsPo.setAutoSetup(autoSetup);
        Integer autoNum = (Integer) tactics.get("autoNum");
        tacticsPo.setAutoNum(autoNum);
        String labelIds = "";
        if(!StringUtils.isEmpty(tactics.get("labelIds"))){
            labelIds = tactics.get("labelIds").toString();
        }else {
            throw new GlobalException(new ResponseException(400, "请选择标签"));
        }
        int count = labelNewDao.updateTactics(tacticsPo);
        if(count>0){
            labelNewDao.deleteTacticsRelByTacticsId(tacticsId);
            String[] labelIdList = labelIds.split(",");
            Map map =new HashMap();
            map.put("tacticsId",tacticsPo.getTacticsId());
            map.put("labelIdList",labelIdList);
            labelNewDao.insertTacticsRel(map);
            return true;
        }
        throw new GlobalException(new ResponseException(400, "添加策略失败"));
    }

    @Override
    public Boolean delTactics(Long tacticsId) {
        int delCount = labelNewDao.deleteTacticsId(tacticsId);
        if(delCount>0){
            //删除关联记录
            labelNewDao.deleteTacticsRelByTacticsId(tacticsId);
            return true;
        }
        throw new GlobalException(new ResponseException(400, "删除策略失败"));
    }

    @Override
    public void updateStatus(Map<String, Object> tactics) {
        TacticsPo tacticsPo = new TacticsPo();
        Long tacticsId = Long.valueOf( tactics.get("tacticsId").toString());
        Integer status = Integer.valueOf(tactics.get("status").toString());
        tacticsPo.setTacticsId(tacticsId);
        tacticsPo.setStatus(status);
        labelNewDao.updateStatus(tacticsPo);
    }

    @Override
    public TacticsPo getTactics(String tacticsId) {
        if (StringUtils.isEmpty(tacticsId)) {
            throw new GlobalException(new ResponseException(400, "策略ID不能为空"));
        }
        return  labelNewDao.getTactics(tacticsId);
    }

    @Override
    public List<Map> enrollAttrList(String enrollId) {
        Map enroll = labelNewDao.enrollList(enrollId);
        List<Map> attrList  = new ArrayList<>();
        if(enroll!=null){
                //查询选项attrIds
                List<Map> attrLists  = labelNewDao.attrList(enroll);
                if(attrLists!=null && attrLists.size()>0){
                    for(Map attr: attrLists){
                        if(!StringUtils.isEmpty(attr.get("attrData"))){
                            String[] attrNames = attr.get("attrData").toString().split(" ");
                            if(attrNames!=null && attrNames.length>0){
                                attr.put("attrNames",attrNames);
                            }
                        }
                    }
                    attrList = attrLists;
                }
        }
        return attrList;
    }

    @Override
    public List<Map> formAttrList(Map<String, Object> map) {
        List<Map> attrList  = new ArrayList<>();
        if(map!=null){
            //查询选项attrIds
            List<Map> attrLists  = (List<Map>) map.get("formAttrVoList");
            if(attrLists!=null && attrLists.size()>0){
                for(Map attr: attrLists){
                    String attrType = "";
                    if(!StringUtils.isEmpty(attr.get("attrType"))){
                        attrType = attr.get("attrType").toString();
                    }
                    if("radio".equals(attrType) || "checkbox".equals(attrType) || "select".equals(attrType)){
                        Map attrMap = new HashMap();
                        attrMap.put("attrId",attr.get("attrId"));
                        attrMap.put("attrTitle",attr.get("attrTitle"));
                        attrMap.put("attrIds",attr.get("itemList"));
                        attrList.add(attrMap);
                    }
                }
            }
        }
        return attrList;
    }
}
